package com.goldgov.jdbc.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.jdbc.service.JdbcConfig;
import com.goldgov.jdbc.service.JdbcConfigService;

@RestController
@RequestMapping("/jdbc")
public class JdbcConfigController {

	@Autowired
	private JdbcConfigService configService;
	
	@PostMapping
	public String saveConfig(JdbcConfig config) {
		configService.saveConfig(config);
		return "SUCCESS";
	}
	
	@DeleteMapping
	public String deleteConfig(@RequestParam("configID") String configID) {
		configService.deleteConfig(configID);
		return "SUCCESS";
	}
	
	@GetMapping("/{configID}")
	public JdbcConfig getConfig(@PathVariable("configID") String configID) {
		return configService.getConfig(configID);
	}
	
	@GetMapping
	public List<JdbcConfig> listConfig(@RequestParam("projectID") String projectID){
		return configService.listConfig(projectID);
	}
	
	@PostMapping("/test")
	public String testConfig(JdbcConfig config){
		return configService.testConfig(config) ? "SUCCESS" : "FAIL";
	}
	
	@GetMapping("/tableName/{configID}")
	public List<String> listTableNames(@PathVariable("configID") String configID){
		return configService.listTableNames(configID);
	}
}
